<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height:40px; width:100%"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col class="box" :title="nodeData.manager">
          {{ nodeData.manager }}</el-col>
        <el-col class="box">
          <el-dropdown @command="commandFn">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!root" command="edit">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!root" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    root: {
    // Boolean布尔型
      type: Boolean,
      default: false
    }
  },
  methods: {
    commandFn(command) {
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },
    async del() {
      // console.log('删除功能')
      try {
        await this.$confirm('你确定要删除吗', '温馨提示', {
          type: 'warning' })
      } catch {
        return
      }
      // 发送请求，删除部门
      await delDepartment(this.nodeData.id)
      this.$message.success('删除部门成功')

      // 通知父组件更新
      this.$emit('getDepartments')
    },
    add() {
      // console.log('添加功能')
      // 显示弹窗
      this.$emit('add', this.nodeData)
      // this.$refs.form.validate(async valid => {
      //   if (!valid) return
      //   if (this.form.id) {
      //     await updateDepartment(this.form)
      //   } else {
      //     // 调用新增接口 添加父部门的id
      //     await addDepartment({ ...this.form, pid: this.treeNode.id })
      //   }
      //   // 通知父组件更新数据
      //   this.$emit('updateDepartment')
      //   // 关闭弹层
      //   this.$emit('update:showDialing', false)
      // })
    },
    edit() {
      // console.log('编辑功能')
      this.$emit('edit', this.nodeData)
      // console.log(this.nodeData)
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  width: 80px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

